package com.fpost.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fpost.dao.GenericDAO;
import com.fpost.domain.User;

public class UserDAO extends GenericDAO<User> {

	private static String GET_USER_BY_USER_NAME = "from User where userName = :userName";

	@Override
	public Long save(User user) throws HibernateException {
		Long userId = null;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			userId = (Long) session.save(user);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return userId;
	}

	@Override
	public void saveOrUpdate(User user) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.saveOrUpdate(user);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
	}

	@Override
	public void delete(long userId) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(get(userId));
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}

	@Override
	public void delete(User user) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(user);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}

	@Deprecated
	public User getByUserId(long userId) throws HibernateException {
		User user = null;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			user = (User) session.get(User.class, userId);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return user;
	}
	
	public User get(long userId) throws HibernateException {
		Session session = getSessionFactory().openSession();
		User user = (User) session.createCriteria(User.class)
				.add(Restrictions.eq("userId", userId)).uniqueResult();

		return user;
	}

	@Deprecated
	public User getByUsername_HQL(String username) throws HibernateException {
		User user = null;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			Query query = session.createQuery(GET_USER_BY_USER_NAME);
			query.setParameter("username", username);
			Object object = query.uniqueResult();
			if (object == null) {
				return null;
			} else {
				user = (User) object;
			}
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return user;
	}

	public User getByUsername(String username) throws HibernateException {
		Session session = getSessionFactory().openSession();
		User user = (User) session.createCriteria(User.class)
				.add(Restrictions.eq("username", username)).uniqueResult();

		return user;
	}
	
	public List<User> getAll() {
		Session session = getSessionFactory().openSession();
		List<User> users = (List<User>) session.createCriteria(User.class).list();
		return users;		
	}

}
